package com.example.jacky.android_easeui.demo;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.example.jacky.android_easeui.R;
import com.example.jacky.android_easeui.base.BaseActivity;
import com.example.jacky.android_easeui.bean.HtmlInterface;

/**
 * Created by jacky on 2016/10/8.
 * sounbus app-developer
 */

public class WebViewActivity extends BaseActivity {
    public static final String TAG = "WebViewActivity";
    private WebView wv;
    private ProgressBar pb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        initView();
        initWebView();
    }

    private void initWebView() {
        // TODO
        //		wv.loadUrl("http://bbs.itheima.com");//1. 网络地址访问一定要加http://
        wv.loadUrl("file:///android_asset/hm47.html");//2. 访问本地的url
//        wv.loadUrl("http://192.168.1.100:8080/html/aa.html");
        WebSettings settings = wv.getSettings();
        settings.setBuiltInZoomControls(true);//3. 放大缩小
        //4.监听加载progress
        wv.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO
                System.out.println("newProgress:" + newProgress);
                //				pb.setMax(100);
                pb.setProgress(newProgress);
                super.onProgressChanged(view, newProgress);
            }
        });
        //5.监听加载过程(开始,结束)
        wv.setWebViewClient(new WebViewClient() {
            /**
             * 开始加载
             */
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO
                pb.setVisibility(View.VISIBLE);
                super.onPageStarted(view, url, favicon);
            }

            /**
             * 结束加载
             */
            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO
                pb.setVisibility(View.GONE);
//                addJs(view);
                super.onPageFinished(view, url);
            }
        });
        //6.设置允许js
        settings.setJavaScriptEnabled(true);
        //7.java->js:在java代码中如何执行一段js
//		wv.loadUrl("javascript:alert('来自java代码的问候')");
        //8.js-->java:在js中调用java本地代码
        wv.addJavascriptInterface(new HtmlInterface(this), "android");
    }
    //9.在网络加载成功后.动态的去执行一段js

    /**
     * 为webview动态执行一段js
     *
     * @param wv
     */
    public void addJs(WebView wv) {
        wv.loadUrl("javascript:" + "(function(){" +
                "var imageList = \"\";" +
                "var imgs = document.getElementsByTagName(\"img\");" +
                "for (var i = 0;i<imgs.length;i++) {" +
                "var img = imgs[i];" +
                "imageList = imageList+img.src+\";\";" +
                "img.onclick = function(){" +
                "window.bindJs.openImg(this.src);" +
                "	}" +
                "}" +
                "window.bindJs.openImgList(imageList);" +
                "})()");
    }

    private void initView() {
        wv = (WebView) findViewById(R.id.wv);
        pb = (ProgressBar) findViewById(R.id.pb);
        pb.setVisibility(View.GONE);
    }

}
